package com.qianfeng.smartdevice.mapper;


//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼            BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  


import com.qianfeng.smartdevice.pojo.Device;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

/**
 * Created by jackiechan on 2020/12/13 13:41
 *
 * @Author jackiechan
 */
public interface DeviceMapper {
    @Insert("INSERT INTO devices (devicesuuid,devicename,address,areaid,direction,num,status,categoryid) VALUES(#{devicesuuid},#{devicename},#{address},#{areaid},#{direction},#{num},#{status},#{categoryid})")
    void AddDevice(Device device);

    @Update("<script>\n" +
            "            UPDATE devices\n" +
            "            <set>\n" +
            "                <if test=\"devicesuuid!=null and devicesuuid!=''\">\n" +
            "                    devicesuuid =#{devicesuuid},\n" +
            "                </if>\n" +
            "                <if test=\"devicename!=null and devicename!=''\">\n" +
            "                    devicename =#{devicename},\n" +
            "                </if>\n" +
            "                <if test=\"addtime!=null\">\n" +
            "                    addtime =#{addtime},\n" +
            "                </if>\n" +
            "                <if test=\"address!=null and address!=''\">\n" +
            "                    address =#{address},\n" +
            "                </if>\n" +
            "                <if test=\"areaid!=null and areaid!=-100\">\n" +
            "                    areaid =#{areaid},\n" +
            "                </if>\n" +
            "                <if test=\"direction!=null and direction!=-100\">\n" +
            "                    direction =#{direction},\n" +
            "                </if>\n" +
            "                <if test=\"num!=null and num!=-100\">\n" +
            "                    num =#{num},\n" +
            "                </if>\n" +
            "                <if test=\"status!=null and status!=-100\">\n" +
            "                    status =#{status},\n" +
            "                </if>\n" +
            "                <if test=\"lastconnecttime!=null\">\n" +
            "                    lastconnecttime =#{lastconnecttime},\n" +
            "                </if>\n" +
            "            </set>\n" +
            "WHERE id = #{id}" +
            "        </script>")
    void updateDevice(Device device);

    @Select("SELECT * FROM devices WHERE d.id = #{id}")
    Device getDeviceById(Long id);

    @Select("  <script>\n" +
            "            SELECT * FROM devices "+
            "            <where>\n" +
            "                <if test=\"address !=null and address !=''\">\n" +
            "\n" +
            "                  and   d.address like concat('%',#{address},'%')\n" +
            "                </if>\n" +
            "\n" +
            "                <if test=\"status !=null and status !=-100\">\n" +
            "                    and   d.status =#{status}\n" +
            "                </if>\n" +
            "\n" +
            "                <if test=\"direction !=null and direction !=-100\">\n" +
            "                    and   d.direction =#{direction}\n" +
            "                </if>\n" +

            "                <if test=\"categoryid !=null and categoryid !=-100\">\n" +
            "                    and   d.categoryid =#{categoryid}\n" +
            "                </if>\n"+

            "                <if test=\"areaid !=null and areaid !=-100\">\n" +
            "                    and   d.areaid =#{areaid}\n" +
            "                </if>\n" +
            "            </where>\n" +
            "\n" +
            "        </script>")
    List<Device> getAllDevices(@Param("address") String address, @Param("status") int status, @Param("direction") int direction, @Param("areaid") int areaid,@Param("categoryid") int categoryid);

    @Update("<script>\n" +
            "\n" +
            "            UPDATE devices SET status='0'\n" +
            "       where id in\n" +
            "            <foreach collection=\"ids\" item=\"id\" separator=\",\" open=\"(\" close=\")\" >\n" +
            "\n" +
            "                #{id}\n" +
            "            </foreach>\n" +
            "\n" +
            "\n" +
            "        </script>")
    void deleteDevices(@Param("ids") List<String> ids);

    @Update("UPDATE devices SET lastconnecttime = #{date} , status =1  WHERE devicesuuid = #{uuid}")
    void updateConnectionTime(@Param("uuid") String uuid, @Param("date") Date date);

    @Update("UPDATE devices SET  status =#{status}  WHERE devicesuuid = #{uuid}")
    void updateoffLine(@Param("uuid") String uuid,@Param("status") String status);


    @Select("SELECT * FROM devices devicesuuid =#{devicesuuid}")
    Device getDeviceByUUId(String  uuid);

    @Select("SELECT categoryid FROM devices WHERE devicesuuid=#{uuid}")
    Long getCategoryIdByUUID(String uuid);

//    @Insert("INSERT INTO devices (devicesuuid,devicename,address,areaid,direction,num,status,categoryid) VALUES(#{devicesuuid},#{devicename},#{address},#{areaid},#{direction},#{num},#{status},#{categoryid})")
//    void AddDevice(Device device);
//
//    @Update("<script>\n" +
//            "            UPDATE devices\n" +
//            "            <set>\n" +
//            "                <if test=\"devicesuuid!=null and devicesuuid!=''\">\n" +
//            "                    devicesuuid =#{devicesuuid},\n" +
//            "                </if>\n" +
//            "                <if test=\"devicename!=null and devicename!=''\">\n" +
//            "                    devicename =#{devicename},\n" +
//            "                </if>\n" +
//            "                <if test=\"addtime!=null\">\n" +
//            "                    addtime =#{addtime},\n" +
//            "                </if>\n" +
//            "                <if test=\"address!=null and address!=''\">\n" +
//            "                    address =#{address},\n" +
//            "                </if>\n" +
//            "                <if test=\"areaid!=null and areaid!=-100\">\n" +
//            "                    areaid =#{areaid},\n" +
//            "                </if>\n" +
//            "                <if test=\"direction!=null and direction!=-100\">\n" +
//            "                    direction =#{direction},\n" +
//            "                </if>\n" +
//            "                <if test=\"num!=null and num!=-100\">\n" +
//            "                    num =#{num},\n" +
//            "                </if>\n" +
//            "                <if test=\"status!=null and status!=-100\">\n" +
//            "                    status =#{status},\n" +
//            "                </if>\n" +
//            "                <if test=\"lastconnecttime!=null\">\n" +
//            "                    lastconnecttime =#{lastconnecttime},\n" +
//            "                </if>\n" +
//            "            </set>\n" +
//            "WHERE id = #{id}" +
//            "        </script>")
//    void updateDevice(Device device);
//
//    @Select("SELECT d.* , a.areaname areaname, c.categoryname categoryname FROM devices d  join areas a   join categories c on d.areaid =a.id and d.categoryid= c.id WHERE d.id = #{id}")
//    Device getDeviceById(Long id);
//
//    @Select("  <script>\n" +
//            "            SELECT d.* , a.areaname areaname, c.categoryname categoryname FROM devices d  join areas a   join categories c\n" +
//            "            on d.areaid =a.id and d.categoryid= c.id \n" +
//            "            <where>\n" +
//            "                <if test=\"address !=null and address !=''\">\n" +
//            "\n" +
//            "                  and   d.address like concat('%',#{address},'%')\n" +
//            "                </if>\n" +
//            "\n" +
//            "                <if test=\"status !=null and status !=-100\">\n" +
//            "                    and   d.status =#{status}\n" +
//            "                </if>\n" +
//            "\n" +
//            "                <if test=\"direction !=null and direction !=-100\">\n" +
//            "                    and   d.direction =#{direction}\n" +
//            "                </if>\n" +
//
//            "                <if test=\"categoryid !=null and categoryid !=-100\">\n" +
//            "                    and   d.categoryid =#{categoryid}\n" +
//            "                </if>\n"+
//
//            "                <if test=\"areaid !=null and areaid !=-100\">\n" +
//            "                    and   d.areaid =#{areaid}\n" +
//            "                </if>\n" +
//            "            </where>\n" +
//            "\n" +
//            "        </script>")
//    List<Device> getAllDevices(@Param("address") String address, @Param("status") int status, @Param("direction") int direction, @Param("areaid") int areaid,@Param("categoryid") int categoryid);
//
//    @Update("<script>\n" +
//            "\n" +
//            "            UPDATE devices SET status='0'\n" +
//            "       where id in\n" +
//            "            <foreach collection=\"ids\" item=\"id\" separator=\",\" open=\"(\" close=\")\" >\n" +
//            "\n" +
//            "                #{id}\n" +
//            "            </foreach>\n" +
//            "\n" +
//            "\n" +
//            "        </script>")
//    void deleteDevices(@Param("ids") List<String> ids);
//
//    @Update("UPDATE devices SET lastconnecttime = #{date} , status =1  WHERE devicesuuid = #{uuid}")
//    void updateConnectionTime(@Param("uuid") String uuid, @Param("date") Date date);
//
//    @Update("UPDATE devices SET  status =#{status}  WHERE devicesuuid = #{uuid}")
//    void updateoffLine(@Param("uuid") String uuid,@Param("status") String status);
//
//
//    @Select("SELECT d.* , a.areaname areaname ,c.categoryname categoryname FROM devices d left join areas a  left join categories c on d.areaid =a.id and d.categoryid= c.id  WHERE d.devicesuuid =#{devicesuuid}")
//    Device getDeviceByUUId(String  uuid);
//
//    @Select("SELECT categoryid FROM devices WHERE devicesuuid=#{uuid}")
//    Long getCategoryIdByUUID(String uuid);
}

